from decimal import Decimal

from turbogears import validators, widgets

from mnemoply import reports
from mnemoply.utils import IdsValidator
from mnemoply.model import ChargeRecord

def calcCharge(sig, month_year):
    lst = list(ChargeRecord.selectBy(student=sig.student, studentAgreement=sig.studentAgreement,
        billingMonth=month_year.month, billingYear=month_year.year))
    return lst[0] if len(lst) > 0 else None

def calcLessons(sig, charged):
    add = 0 if charged.realHours % sig.group.lessonPeriod == 0 else 1

    return (charged.realHours / sig.group.lessonPeriod) + add

class OplataReport(reports.RDSReport):
    name = _("Payment")
    tag = "Oplata"
    report_type = reports.RDSReportType.EXCEL2
    template_module = "mnemoply.templates.reports.oplata"

    fields = [
        widgets.HiddenField(name="ids", label=_("IDs"),
            validator=IdsValidator),
        widgets.TextField(name='charge_month', label=_('Charge month'),
            validator=validators.Int(not_empty=True)),
        widgets.TextField(name='charge_year', label=_('Charge year'),
            validator=validators.Int(not_empty=True)),
    ]

    pass_functions = (calcCharge, calcLessons)

exposed = OplataReport()
